package com.zzwtec.information.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.zzwtec.information.dao.BaseDao;
import com.zzwtec.interfaces.Page;
import com.zzwtec.information.model.Advertiser;

@Repository(value = "advertiserDao")
public class AdvertiserDao extends BaseDao<Advertiser>{
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	private final String TABLE_NAME = "advertiser";
	
	
	/**
	 * 分页查询
	 * @param page
	 * @return 数据模型列表
	 */
	@Transactional(readOnly = true)
	public Page<Advertiser> selectPage(Page<Advertiser> page) {
		return super.selectPage(Advertiser.class, TABLE_NAME, page, new AdvertiserRowMapper());
	}
	
	/**
	 * 全查询，一般不推荐使用，除非数据量不大
	 * 
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<Advertiser> selectAll() {		
		return super.selectAll(TABLE_NAME, new AdvertiserRowMapper());
	}

	/**
	 * 获取一条记录
	 * 
	 * @param id
	 * @return
	 */
	@Transactional(readOnly = true)
	public Advertiser selectOne(String id) {		
		return super.selectOne(TABLE_NAME, id, new AdvertiserRowMapper());

	}

	

	

	/**
	 * 更新单表数据
	 * 
	 * @param sql
	 * @return
	 */
	public int update(String sql) {
		return jdbcTemplate.update(sql);
	}

	/**
	 * 批量更新单表数据
	 * 
	 * @param sql
	 * @param batchArgs
	 * @return
	 */
	public int[] batchUpdate(String sql) {
		return jdbcTemplate.batchUpdate(sql);
	}

	/**
	 * 更新区域
	 * @param bean
	 * @return
	 */
	public int update(Advertiser bean) {
		String sql = "update advertiser set name= ?,address= ?,phone= ?,size= ?,rest_size= ? where id= ?"; 
		Object[] params = new Object[]{bean.getName(),bean.getAddress(),bean.getPhone(),bean.getSize(),bean.getRestSize(),bean.getId()};;
		return update(sql, params);
	}
	
	/**
	 * 更新单表数据
	 * 
	 * @param sql
	 * @param params
	 * @return
	 */
	public int update(String sql, Object... params) {
		return jdbcTemplate.update(sql, params);
	}

	/**
	 * 批量更新单表数据,支持sql预处理语句参数
	 * 
	 * @param sql
	 * @param batchArgs
	 * @return
	 */
	public int[] batchUpdate(String sql, List<Object[]> batchArgs) {
		return jdbcTemplate.batchUpdate(sql, batchArgs);
	}

	/**
	 * 添加区域
	 * @param bean
	 * @return
	 */
	public int insert(Advertiser bean) {
		String sql = "insert into advertiser (id,name,address,phone,size,rest_size) values (?,?,?,?,?,?)";
		Object[] params = new Object[]{bean.getId(),bean.getName(),bean.getAddress(),bean.getPhone(),bean.getSize(),bean.getRestSize()};;		
		return insert(sql, params);
	}
	
	

	/**
     * 按id删除数据
     * @param id
     * @return
     */
	public int deleteById(String id) {
		String sql = "delete from advertiser where id = ?";		
		Object[] params = new Object[]{id};
		return delete(sql, params);
	}
	
	/**
	 * 按ids删除数据
     * @param ids
     * @return 受影响行数
	 */
	public int deleteByIds(String[] ids) {
		return super.deleteByIds(TABLE_NAME, ids);
	}
	
	/**
	 * 删除数据 
	 * @param sql
	 * @param params
	 * @return
	 */
	private int delete(String sql, Object... params) {
		if (sql == null || !sql.startsWith("delete")) {
			throw new RuntimeException("不是合法的删除数据sql语句: " + sql);
		}
		return jdbcTemplate.update(sql, params);
	}

}

class AdvertiserRowMapper implements RowMapper<Advertiser> {
	@Override
	public Advertiser mapRow(ResultSet rs, int rowNum) throws SQLException {
		Advertiser bean = new Advertiser();		
		bean.setId(rs.getString("id"));
		bean.setName(rs.getString("name"));
		bean.setAddress(rs.getString("address"));
		bean.setPhone(rs.getString("phone"));
		bean.setSize(rs.getLong("size"));
		bean.setRestSize(rs.getLong("rest_size"));
		return bean;
	}

}